﻿#include<iostream>
#include<map>

void printMap(std::map<int, int>& m)
{
	for (std::map<int, int>::iterator it = m.begin(); it != m.end(); ++it)
	{
		std::cout << "key = " << it->first << "   value = " << (*it).second << '\n';
	}
	std::cout << '\n';
}

void test01()
{
	std::map<int, int> m1;

	m1.insert(std::pair<int, int>(1, 10));
	m1.insert(std::pair<int, int>(3, 30));
	m1.insert(std::pair<int, int>(2, 20));
	m1.insert(std::pair<int, int>(5, 50));
	m1.insert(std::pair<int, int>(4, 40));
	
	if (m1.empty())
	{
		std::cout << "m1为空" << '\n';
	}
	else
	{
		std::cout << "m1不为空" << '\n';
		printMap(m1);
		std::cout << "m1的大小为：" << m1.size() << '\n';
	}
}

void test02()
{
	std::map<int, int> m1;

	m1.insert(std::pair<int, int>(1, 10));
	m1.insert(std::pair<int, int>(3, 30));
	m1.insert(std::pair<int, int>(2, 20));
	m1.insert(std::pair<int, int>(5, 50));
	m1.insert(std::pair<int, int>(4, 40));

	std::map<int, int> m2;

	m2.insert(std::pair<int, int>(1, 50));
	m2.insert(std::pair<int, int>(3, 30));
	m2.insert(std::pair<int, int>(2, 40));
	m2.insert(std::pair<int, int>(5, 10));
	m2.insert(std::pair<int, int>(4, 20));

	std::cout << "交换前：" << '\n';
	printMap(m1);
	printMap(m2);


	std::cout << "交换后：" <<'\n';
	m1.swap(m2);
	printMap(m1);
	printMap(m2);
}

int main()
{
	test01();
	std::cout << "----------------------" << '\n';
	test02();

	return 0;
}
